package com.ytjj.qmyx.mall.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.ytjj.common.constants.AlipayConstants;
import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.mall.enums.PayVersionEnum;
import com.ytjj.qmyx.mall.model.PayConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

@Slf4j
@Component
public class AlipayFactoryUtils {

    @Resource
    private AlipayConstants alipayConstants;


    public AlipayClient getAlipayClient(PayConfig payConfig, String childId) {
        // 向支付宝发送支付请求
        // 1. 封装RSA签名方式
        //获得初始化的AlipayClient
        AlipayClient alipayClient;
        if (Objects.equals(PayVersionEnum.cert.name(),payConfig.getVersion())){
            CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
            certAlipayRequest.setServerUrl(alipayConstants.getGATEWAY_URL());  //gateway:支付宝网关（固定）https://openapi.alipay.com/gateway.do
            certAlipayRequest.setAppId(payConfig.getAppid());  //APPID 即创建应用后生成,详情见创建应用并获取 APPID
            certAlipayRequest.setPrivateKey(payConfig.getSecret());  //开发者应用私钥，由开发者自己生成
            certAlipayRequest.setFormat(alipayConstants.getFORMAT());  //参数返回格式，只支持 json 格式
            certAlipayRequest.setCharset(alipayConstants.getCHARSET());  //请求和签名使用的字符编码格式，支持 GBK和 UTF-8
            certAlipayRequest.setSignType(alipayConstants.getSIGNTYPE());  //商户生成签名字符串所使用的签名算法类型，目前支持 RSA2 和 RSA，推荐商家使用 RSA2。
            certAlipayRequest.setCertPath(payConfig.getCertPath()/*alipayCertConstants.getAPP_CERT_PATH()*/); //应用公钥证书路径（app_cert_path 文件绝对路径）
            certAlipayRequest.setAlipayPublicCertPath(payConfig.getPrivateCertPath()/*alipayCertConstants.getALIPAY_CERT_PATH()*/); //支付宝公钥证书文件路径（alipay_cert_path 文件绝对路径）
            certAlipayRequest.setRootCertPath(payConfig.getPrivateKeyPath()/*alipayCertConstants.getALIPAY_ROOT_CERT_PATH()*/);  //支付宝CA根证书文件路径（alipay_root_cert_path 文件绝对路径）

            try {
                alipayClient = new DefaultAlipayClient(certAlipayRequest);
            } catch (AlipayApiException e) {
                log.info("===初始化支付宝客户端异常===:", e);
                throw new ApiException(e.getMessage());
            }

        }else {
            if (StringUtils.isNotBlank(childId) && "1".equals(childId)) {
                alipayClient = new DefaultAlipayClient
                        (alipayConstants.getGATEWAY_URL(),
                                alipayConstants.getCHILD_JSAPI_APPID(),
                                alipayConstants.getCHILD_JSAPI_RSA_PRIVATE_KEY(),
                                alipayConstants.getFORMAT(),
                                alipayConstants.getCHARSET(),
                                alipayConstants.getCHILD_JSAPI_ALIPAY_PUBLIC_KEY(),
                                alipayConstants.getSIGNTYPE());
            } else {
                alipayClient = new DefaultAlipayClient
                        (alipayConstants.getGATEWAY_URL(),
                                alipayConstants.getJSAPI_APPID(),
                                alipayConstants.getJSAPI_RSA_PRIVATE_KEY(),
                                alipayConstants.getFORMAT(),
                                alipayConstants.getCHARSET(),
                                alipayConstants.getJSAPI_ALIPAY_PUBLIC_KEY(),
                                alipayConstants.getSIGNTYPE());
            }
        }
        return alipayClient;
    }

    public AlipayClient getAlipayClient(String childId) {
        AlipayClient alipayClient;
        if (StringUtils.isNotBlank(childId) && "1".equals(childId)) {
            alipayClient = new DefaultAlipayClient
                    (alipayConstants.getGATEWAY_URL(),
                            alipayConstants.getCHILD_JSAPI_APPID(),
                            alipayConstants.getCHILD_JSAPI_RSA_PRIVATE_KEY(),
                            alipayConstants.getFORMAT(),
                            alipayConstants.getCHARSET(),
                            alipayConstants.getCHILD_JSAPI_ALIPAY_PUBLIC_KEY(),
                            alipayConstants.getSIGNTYPE());
        } else {
            alipayClient = new DefaultAlipayClient
                    (alipayConstants.getGATEWAY_URL(),
                            alipayConstants.getJSAPI_APPID(),
                            alipayConstants.getJSAPI_RSA_PRIVATE_KEY(),
                            alipayConstants.getFORMAT(),
                            alipayConstants.getCHARSET(),
                            alipayConstants.getJSAPI_ALIPAY_PUBLIC_KEY(),
                            alipayConstants.getSIGNTYPE());
        }
        return alipayClient;
    }


}
